<template>
  <div class="app-container">
    <!-- 顶部控制台 -->
    <div class="filter-container">
<!--      <el-button class="filter-item" type="primary" size="mini" icon="el-icon-share" @click="handleOpenSavePanel">添加任务</el-button>-->
    </div>
    <!-- 数据列表 -->
    <el-table :data="responseData.tableList"
              border style="width: 100%;"
              v-loading="pageData.loading"
              element-loading-text="拼命加载中"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(0, 0, 0, 0.8)"
              :header-cell-style="{ background: '#545C64', color: '#ffffff' }"
              @selection-change="handleTableSelection">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column fixed="left" prop="taskId" label="规则ID" width="280" align="center"/>
      <el-table-column prop="taskName" label="任务名称" width="160" align="center">
        <template slot-scope="scope">
          <span><el-tag type="primary">{{scope.row.taskName}}</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column prop="taskPlat" label="所属平台" width="100" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.taskPlat === 1"><el-tag type="success">APP</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column prop="taskType" label="任务类型" width="160" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.taskType === 1"><el-tag type="primary">每日任务</el-tag></span>
          <span v-if="scope.row.taskType === 2"><el-tag type="warning">新手任务</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column prop="taskClassifyType" label="任务定位" width="200" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.taskClassifyType === 1"><el-tag type="primary">签到任务(每日签到)</el-tag></span>
          <span v-if="scope.row.taskClassifyType === 2"><el-tag type="warning">阅读任务(阅读文章)</el-tag></span>
          <span v-if="scope.row.taskClassifyType === 3"><el-tag type="success">收听任务(收听音频)</el-tag></span>
          <span v-if="scope.row.taskClassifyType === 4"><el-tag type="info">观看任务(观看视频)</el-tag></span>
          <span v-if="scope.row.taskClassifyType === 5"><el-tag type="danger">提问任务(进行提问)</el-tag></span>
          <span v-if="scope.row.taskClassifyType === 6"><el-tag type="primary">回答任务(进行回答)</el-tag></span>
          <span v-if="scope.row.taskClassifyType === 7"><el-tag type="warning">评论任务(评论内容)</el-tag></span>
          <span v-if="scope.row.taskClassifyType === 8"><el-tag type="success">分享任务(分享内容)</el-tag></span>
          <span v-if="scope.row.taskClassifyType === 9"><el-tag type="info">注册任务(用户注册)</el-tag></span>
          <span v-if="scope.row.taskClassifyType === 10"><el-tag type="danger">资料任务(完善资料)</el-tag></span>
          <span v-if="scope.row.taskClassifyType === 11"><el-tag type="primary">兴趣任务(兴趣标签)</el-tag></span>
          <span v-if="scope.row.taskClassifyType === 12"><el-tag type="warning">绑定任务(绑定账户)</el-tag></span>
          <span v-if="scope.row.taskClassifyType === 13"><el-tag type="success">点赞任务(内容点赞)</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column prop="taskFinishWay" label="达成方式" width="160" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.taskFinishWay === 1"><el-tag type="primary">点击/自动</el-tag></span>
          <span v-if="scope.row.taskFinishWay === 2"><el-tag type="warning">页面停留</el-tag></span>
          <span v-if="scope.row.taskFinishWay === 3"><el-tag type="success">评论内容</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column prop="taskFinishWayTerm" label="达成条件" width="160" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.taskFinishWay === 1"><el-tag type="info">无达成条件</el-tag></span>
          <span v-if="scope.row.taskFinishWay === 2"><el-tag type="success">页面停留 [{{scope.row.taskFinishWayTerm}}] 秒</el-tag></span>
          <span v-if="scope.row.taskFinishWay === 3"><el-tag type="primary">评论内容 [{{scope.row.taskFinishWayTerm}}] 字</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column prop="taskStatus" label="审核状态" width="80" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.taskStatus === 0"><el-tag type="danger">预设</el-tag></span>
          <span v-if="scope.row.taskStatus === 1"><el-tag type="success">发布</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column prop="taskIntegralCount" label="赠送积分" width="160" align="center">
        <template slot-scope="scope">
          <span><el-tag type="success">赠送 [ <span>{{scope.row.taskIntegralCount}}</span> ] 积分值</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column prop="taskExpertCount" label="赠送经验" width="160" align="center">
        <template slot-scope="scope">
          <span><el-tag type="warning">赠送 [ <span>{{scope.row.taskExpertCount}}</span> ] 经验值</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column prop="taskAwardCount" label="可达成次数" width="180" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.taskAwardCount === -1"><el-tag type="danger">达成次数无限制</el-tag></span>
          <span v-if="scope.row.taskType === 2"><el-tag type="warning">新手任务只能达成一次</el-tag></span>
          <span v-if="scope.row.taskType !== 2 && scope.row.taskAwardCount !== -1"><el-tag type="success">每日可完成 [{{scope.row.taskAwardCount}}] 次</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column prop="taskInfo" label="任务说明" width="280" align="center">
        <template slot-scope="scope">
          <span><el-tag type="info">{{scope.row.taskInfo}}</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="240" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleOpenEditPanel(scope)">编辑</el-button>
          <el-button v-if="scope.row.taskStatus === 0" size="mini" type="success" @click="TaskTableChangeStatusButtonClickEvent(scope)">发布</el-button>
          <el-button v-if="scope.row.taskStatus === 1" size="mini" type="info" @click="TaskTableChangeStatusButtonClickEvent(scope)">下架</el-button>
          <el-button type="danger" size="mini"  @click="TaskTableDeleteButtonClickEvent(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      :current-page="form.pageNum"
      :page-size="form.pageSize"
      :total="form.total"
      :page-sizes="[12, 50, 100, 200]"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange">
    </el-pagination>
    <!-- 新增/修改隐藏域 -->
    <el-dialog title="任务配置规则新增页" :visible.sync="pageData.savePanelStatus" size="small"
               :before-close="handleCloseSavePanel">
      <el-form ref="form.task" :model="form.task" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-row>
              <el-col :span="24">
                <el-form-item label="任务名称">
                  <el-input v-model="form.task.taskName"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="所属平台">
                  <el-radio v-model="form.task.taskPlat" label="1">APP</el-radio>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="任务类型">
                  <el-radio v-model="form.task.taskType" :label="1">每日任务</el-radio>
                  <el-radio v-model="form.task.taskType" :label="2">新手任务</el-radio>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="审核状态">
                  <el-radio v-model="form.task.taskStatus" :label="0">预设</el-radio>
                  <el-radio v-model="form.task.taskStatus" :label="1">发布</el-radio>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务图标">
              <el-upload
                class="avatar-uploader"
                action="/api/cms/common/uploadFile"
                :show-file-list="false"
                :before-upload="handleHomeImageUploadBefore"
                :on-success="handleHomeImageUploadSuccess">
                <img v-if="form.task.taskImage" :src="form.task.taskImage" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon" style="line-height: 178px"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="任务定位">
              <el-radio v-model="form.task.taskClassifyType" :label="1">签到</el-radio>
              <el-radio v-model="form.task.taskClassifyType" :label="2">阅读</el-radio>
              <el-radio v-model="form.task.taskClassifyType" :label="3">收听</el-radio>
              <el-radio v-model="form.task.taskClassifyType" :label="4">观看</el-radio>
              <el-radio v-model="form.task.taskClassifyType" :label="5">提问</el-radio>
              <el-radio v-model="form.task.taskClassifyType" :label="6">回答</el-radio>
            </el-form-item>
            <el-form-item label="">
              <el-radio v-model="form.task.taskClassifyType" :label="7">评论</el-radio>
              <el-radio v-model="form.task.taskClassifyType" :label="8">分享</el-radio>
              <el-radio v-model="form.task.taskClassifyType" :label="9">注册</el-radio>
              <el-radio v-model="form.task.taskClassifyType" :label="10">资料</el-radio>
              <el-radio v-model="form.task.taskClassifyType" :label="11">兴趣</el-radio>
              <el-radio v-model="form.task.taskClassifyType" :label="12">绑定</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="15">
            <el-form-item label="达成方式">
              <el-radio v-model="form.task.taskFinishWay" :label="1">点击/自动</el-radio>
              <el-radio v-model="form.task.taskFinishWay" :label="2">页面停留</el-radio>
              <el-radio v-model="form.task.taskFinishWay" :label="3">评论内容</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item label="达成条件">
              <el-input v-model="form.task.taskFinishWayTerm" placeholder="页面停留添加分钟数，评论内容添加字数，其它为-1"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="赠送积分">
              <el-input v-model="form.task.taskIntegralCount"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="赠送经验">
              <el-input v-model="form.task.taskExpertCount"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="达成次数">
              <el-input v-model="form.task.taskAwardCount" placeholder="可奖励次数(-1为无限制,每日完成次数,如类型为新手任务的时候,该任务只可完成一次)"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="任务说明">
              <el-input v-model="form.task.taskInfo" type="textarea" :rows="2"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="展示顺序">
              <el-input v-model="form.task.taskShowIndex"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="跳转位置">
              <el-radio v-model="form.task.taskRouterType" :label="0">无跳转</el-radio>
              <el-radio v-model="form.task.taskRouterType" :label="1">首页</el-radio>
              <el-radio v-model="form.task.taskRouterType" :label="2">案例</el-radio>
              <el-radio v-model="form.task.taskRouterType" :label="3">视频</el-radio>
              <el-radio v-model="form.task.taskRouterType" :label="4">研修</el-radio>
              <el-radio v-model="form.task.taskRouterType" :label="5">法律</el-radio>
              <el-radio v-model="form.task.taskRouterType" :label="6">聊政采</el-radio>
              <el-radio v-model="form.task.taskRouterType" :label="7">问答</el-radio>
              <el-radio v-model="form.task.taskRouterType" :label="8">单位信息</el-radio>
              <el-radio v-model="form.task.taskRouterType" :label="9">用户兴趣</el-radio>
              <el-radio v-model="form.task.taskRouterType" :label="10">三方绑定</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button type="primary" @click="handleSaveButtonClick">保存任务</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  // 组件引入
  // 方法工具引入
  import { PermissionsToDetectFormUser } from '@/utils/PermissionsDetect';
  import { getPermission } from '@/utils/UserCookies';
  // 接口引入
  import { loadCmsTaskList } from "@/api/operation";
  export default {
    name: "task",
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
      return {
        pageData:{
          loading: false,
          savePanelStatus: false,
          savePanelType: 0
        },// 页面配置数据
        form:{
          pageNum: 1,
          pageSize: 12,
          total: 0,
          selection: [],
          task: {
            taskId: "",
            taskName: "",
            taskImage: "",
            taskPlat: "",
            taskInfo: "",
            taskType: 1,
            taskClassifyType: 1,
            taskFinishWay: 1,
            taskFinishWayTerm: -1,
            taskStatus: 0,
            taskIntegralCount: 0,
            taskExpertCount: 0,
            taskAwardCount: -1,
            taskShowIndex: 0,
            taskRouterType: 0
          },
        },// 页面表单
        formRules: {},// 表单验证
        responseData: {},// 接口响应数据
      }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods:{
      // 事件函数
      handlePageInit(){
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsTaskList();// 读取路由角色列表
      },// 页面初始化事件
      handleCurrentChange(val){
        this.form.pageNum = val;// 更换页码
        this.handlePageInit();// 进行数据加载
      },// 页码切换事件
      handleSizeChange(val){
        this.form.pageSize = val;// 更换页面大小
        this.handlePageInit();// 进行数据加载
      },// 页面size切换事件
      handleTableSelection(val){
        let list = [];// 初始化多选数组
        for (let i=0; i<val.length; i++){
          list.push(val[i].orderId);
        }// 遍历多选的列表，将ID拼入数组中
        this.form.selection = list;// 进行多选值的切换
      },// 表格多选事件
      handleOpenSavePanel(){
        // 判断当前登录用户所拥有的的操作权限
        if (PermissionsToDetectFormUser(getPermission(), "280001")){
          // 初始化表单
          this.form.task.taskName = "";
          this.form.task.taskImage = "";
          this.form.task.taskPlat = "1";
          this.form.task.taskType = 1;
          this.form.task.taskClassifyType = 1;
          this.form.task.taskFinishWay = 1;
          this.form.task.taskFinishWayTerm = -1;
          this.form.task.taskStatus = 0;
          this.form.task.taskIntegralCount = 0;
          this.form.task.taskExpertCount = 0;
          this.form.task.taskAwardCount = -1;
          this.form.task.taskInfo = "";
          this.form.task.taskShowIndex = 0;
          this.form.task.taskRouterType = 0;
          // 打开新增面板
          this.pageData.savePanelStatus = true;
          this.pageData.savePanelType = 1;
        } else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开新增面板
      handleCloseSavePanel(){
        this.$confirm("确认关闭？").then(response => {
          // 初始化表单
          this.form.task.taskName = "";
          this.form.task.taskImage = "";
          this.form.task.taskPlat = "1";
          this.form.task.taskType = 1;
          this.form.task.taskClassifyType = 1;
          this.form.task.taskFinishWay = 1;
          this.form.task.taskFinishWayTerm = -1;
          this.form.task.taskStatus = 0;
          this.form.task.taskIntegralCount = 0;
          this.form.task.taskExpertCount = 0;
          this.form.task.taskAwardCount = -1;
          this.form.task.taskInfo = "";
          this.form.task.taskShowIndex = 0;
          this.form.task.taskRouterType = 0;
          // 打开新增面板
          this.pageData.savePanelStatus = false;
          this.pageData.savePanelType = 0;
        }).catch(err => {
          console.log(err);
        });
      },// 关闭新增面板
      handleOpenEditPanel(scope){
        // 判断当前登录用户所拥有的的操作权限
        if (PermissionsToDetectFormUser(getPermission(), "280003")){
          // 将等级数据最新等级数据补充到表单中
          this.form.task.taskName = scope.row.taskName;
          this.form.task.taskImage = scope.row.taskImage;
          this.form.task.taskPlat = scope.row.taskPlat;
          this.form.task.taskType = scope.row.taskType;
          this.form.task.taskClassifyType = scope.row.taskClassifyType;
          this.form.task.taskFinishWay = scope.row.taskFinishWay;
          this.form.task.taskFinishWayTerm = scope.row.taskFinishWayTerm;
          this.form.task.taskStatus = scope.row.taskStatus;
          this.form.task.taskIntegralCount = scope.row.taskIntegralCount;
          this.form.task.taskExpertCount = scope.row.taskExpertCount;
          this.form.task.taskAwardCount = scope.row.taskAwardCount;
          this.form.task.taskInfo = scope.row.taskInfo;
          this.form.task.taskShowIndex = scope.row.taskShowIndex;
          this.form.task.taskRouterType = scope.row.taskRouterType;
          // 打开面板
          this.pageData.savePanelStatus = true;
          this.pageData.savePanelType = 2;
        } else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开修改面板
      handleHomeImageUploadBefore(file){
        const fileSize = file.size / 1024 / 1024 < 2;
        if (!fileSize) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return fileSize;
      },//图片上传之前的验证
      handleHomeImageUploadSuccess(response){
        this.form.medal.medalImage = response.data;
      },// 图片上传成功的方法
      handleSaveButtonClick(){
        // 判断当前登录用户所拥有的的操作权限
        if (this.pageData.savePanelType === 1) {
          if (!PermissionsToDetectFormUser(getPermission(), "280001")){
            this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
            return false;
          }
        } else if (this.pageData.savePanelType === 2) {
          if (!PermissionsToDetectFormUser(getPermission(), "280003")){
            this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
            return false;
          }
        }
        // 当前操作权限充足，进行敏感操作提示
        this.$confirm('此操作将会保存当前数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 同一操作，执行具体流程
          if (this.pageData.savePanelType === 1) {
            this.saveCmsTaskData();
          } else if (this.pageData.savePanelType === 2) {
            this.updateCmsTaskData();
          }
        }).catch(close => {
          console.log(close);
          this.$message.info('已取消操作');
        });
      },// 保存勋章配置数据
      // 接口
      loadCmsTaskList(){
        loadCmsTaskList(this.form).then(response => {
          if (response.code === "200"){
            this.pageData.loading = false;
            this.responseData.tableList = response.data.list;
            this.form.total = response.data.total;
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 获取数据列表
      saveCmsTaskData(){
        saveCmsTaskData(this.form.task).then(response => {
          if (response.code === "200"){
            // 初始化表单
            this.form.task.taskName = "";
            this.form.task.taskImage = "";
            this.form.task.taskPlat = "1";
            this.form.task.taskType = 1;
            this.form.task.taskClassifyType = 1;
            this.form.task.taskFinishWay = 1;
            this.form.task.taskFinishWayTerm = -1;
            this.form.task.taskStatus = 0;
            this.form.task.taskIntegralCount = 0;
            this.form.task.taskExpertCount = 0;
            this.form.task.taskAwardCount = -1;
            this.form.task.taskInfo = "";
            this.form.task.taskShowIndex = 0;
            this.form.task.taskRouterType = 0;
            // 打开新增面板
            this.pageData.savePanelStatus = false;
            this.pageData.savePanelType = 0;
            this.handlePageInit()
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 保存勋章数据
      updateCmsTaskData(){
        updateCmsTaskData(this.form.task).then(response => {
          if (response.code === "200"){
            // 初始化数据
            this.form.task.taskName = "";
            this.form.task.taskImage = "";
            this.form.task.taskPlat = "1";
            this.form.task.taskType = 1;
            this.form.task.taskClassifyType = 1;
            this.form.task.taskFinishWay = 1;
            this.form.task.taskFinishWayTerm = -1;
            this.form.task.taskStatus = 0;
            this.form.task.taskIntegralCount = 0;
            this.form.task.taskExpertCount = 0;
            this.form.task.taskAwardCount = -1;
            this.form.task.taskInfo = "";
            this.form.task.taskShowIndex = 0;
            this.form.task.taskRouterType = 0;
            // 打开新增面板
            this.pageData.savePanelStatus = false;
            this.pageData.savePanelType = 0;
            this.handlePageInit()
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 更新勋章数据
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created: function () {
      let _this = this;
      setTimeout(function () {
        _this.handlePageInit();
      }, 800);
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  };
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
